package com.lc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lc.bean.Goods;
import com.lc.bean.Order;
import com.lc.bean.OrderDetail;
import com.lc.enumd.OrderTypeEnum;
import com.lc.enumd.TypeEnum;
import com.lc.mapper.GoodsMapper;
import com.lc.mapper.OrderDetailMapper;
import com.lc.mapper.OrderMapper;
import com.lc.service.OrderService;
import com.lc.vo.CgOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Transactional
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    OrderDetailMapper orderDetailMapper;


    @Override
    public PageInfo list(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        return new PageInfo(orderMapper.list());
    }

    @Override
    public List<Order> assignlist() {
        return orderMapper.assignlist();
    }

    @Override
    public List<Order> transportlist() {
        return orderMapper.transportlist();
    }

    @Override
    public List<Goods> selectGoodsListBySupplierId(Integer supplierid) {
        return orderMapper.selectGoodsListBySupplierId(supplierid);
    }

    @Override
    public void saveCgOrder(CgOrderVo cgOrderVo) {
        // 1、先保存数据到tb_order表
        // CgOrderVo(supplieruuid=23, checker=7, items=[7-100, 8-200])
        Order order = new Order();
        order.setOrdernum(UUID.randomUUID().toString());
        order.setCreater(cgOrderVo.getLoginemp().getUuid());
        order.setCreatetime(new Date());
        order.setChecker(cgOrderVo.getChecker());
        order.setOrdertype(OrderTypeEnum.CG_ORDER.getType());  // 枚举的应用
        order.setType(1);   // 待审批
        order.setSupplieruuid(cgOrderVo.getSupplieruuid());
        order.setStoreuuid(cgOrderVo.getStoreuuid());

        Integer totalCount = 0;
        Double totalFee = 0d;

        for (String item : cgOrderVo.getItems()) {
            String[] split = item.split("-");
            Integer id = Integer.parseInt(split[0]);
            Integer count = Integer.parseInt(split[1]);

            totalCount +=count;

            // 依次查询每个商品的单价，计算
            Goods byId = goodsMapper.findById(id);
            totalFee += byId.getInprice() * count;
        }

        order.setTotalnum(totalCount);
        order.setTotalprice(totalFee);
        // 需要保存并返回主键
        orderMapper.saveCgOrder(order);


        // 2、保存数据到tb_orderdetail表
        // items=[7-100, 8-200]
        for (String item : cgOrderVo.getItems()) {
            String[] split = item.split("-");
            Integer id = Integer.parseInt(split[0]);
            Integer count = Integer.parseInt(split[1]);

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setGoodsuuid(id);

            // 依次查询每个商品的单价，计算
            Goods byId = goodsMapper.findById(id);
            orderDetail.setPrice(byId.getInprice());

            // 订单号？？？？
            orderDetail.setOrderuuid(order.getUuid());

            orderDetail.setNum(count);

            orderDetailMapper.save(orderDetail);
        }

    }

    @Override
    public List<Order> approveList(Integer uuid) {
        return orderMapper.approveList(uuid);
    }

    @Override
    public void accept(Integer orderuuid) {
        Order order =new Order();
        order.setUuid(orderuuid);
        order.setChecktime(new Date());
        orderMapper.accept(order);
    }

    @Override
    public void assign(Integer orderuuid, Integer completer) {
        Order order =new Order();
        order.setUuid(orderuuid);
        order.setCompleter(completer);
        order.setType(TypeEnum.DRK.getType());
        orderMapper.assign(order);
    }
}
